package com.igame.Service.impl;

import com.igame.Service.FollowService;
import com.igame.common.Result;
import com.igame.entity.User;
import com.igame.mapper.FollowMapper;
import com.igame.mapper.UserMapper;
import com.igame.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;

import static com.igame.Service.impl.UserServiceImpl.extractBeforeCaret;

@Service
public class FollowImpl implements FollowService {

    @Autowired
    FollowMapper followMapper;

    @Autowired
    UserMapper userMapper;
    @Override
    public Result<List<UserVo>> myConcern(Integer id) {

        List<Integer> ids = followMapper.selectFollowUser(id);
        List<UserVo> userVoList =new ArrayList<>();
        for (Integer integer : ids) {
            User user = userMapper.selectUserById(integer);
            String caret = extractBeforeCaret(user.getAddress());
            UserVo userVo =new UserVo();
            BeanUtils.copyProperties(user,userVo);
            userVo.setAddress(caret);
            LocalDate userDtoBirth = user.getCreateTime();
            LocalDate now = LocalDate.now();
            Period period = Period.between(userDtoBirth, now);
            userVo.setAccountAge(period.getYears());
            userVoList.add(userVo);
        }
        return Result.success(userVoList);
    }
}
